<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------

namespace app\admin\controller;

use \think\Controller;
use think\Db;
use app\admin\model\MemberType as MemberTypeModel;

class Membertype extends Controller
{

    /**
     * 获取小数点位数
     */
    private function _getFloatLength($num)
    {
        $count = 0;

        $temp = explode('.', $num);

        if (sizeof($temp) > 1) {
            $decimal = end($temp);
            $count = strlen($decimal);
        }

        return $count;
    }

    /**
     * 会员角色列表
     */
    public function index()
    {
        $model = new MemberTypeModel();
        //会员角色
        $info = $model->order('creattime desc')->paginate(20);
        $classify_info = Db::name('classify')->where("classify_status",1)->select();
        $classify_arr = array();
        foreach ($classify_info as $v) {
            $classify_arr[$v['classify_id']] = $v['classify_name'];
        }
        $classify_arr[304] = "接送机";
        $classify_arr[305] = "包车";

        foreach ($info as $k=>$v) {
            //拆分 折扣率字符串
            $discoun_per_list = $v['discoun_per_list'];
            $discoun_show_per_str = '<br>';
            if($discoun_per_list && $discoun_per_list!==''){
                $discoun_per_arr = explode(",",$discoun_per_list);
                foreach ($discoun_per_arr as $per_value){
                    $per_arr = explode("_",$per_value);
                    $per_classify_id = $per_arr[0];
                    $per = $per_arr[1];
                    $all_per_arr[$per_classify_id] = $per;
                    if(isset($classify_arr[$per_classify_id])){
                        $discoun_show_per_str .= $classify_arr[$per_classify_id]." ".$per."<br><br>";
                    }
                }
            }
            $info[$k]['discoun_per'] = $discoun_show_per_str;

            //拆分 返佣利率字符串
            $commission_per_list = $v['commission_per_list'];
            $commission_show_per_str = '<br>';
            if($commission_per_list && $commission_per_list!==''){
                $commission_per_arr = explode(",",$commission_per_list);
                foreach ($commission_per_arr as $per_value){
                    $per_arr = explode("_",$per_value);
                    $per_classify_id = $per_arr[0];
                    $per = $per_arr[1];
                    $all_per_arr[$per_classify_id] = $per;
                    if(isset($classify_arr[$per_classify_id])){
                        $commission_show_per_str .= $classify_arr[$per_classify_id]." ".$per."<br><br>";
                    }
                }
            }
            $info[$k]['per_str'] = $commission_show_per_str;
        }

        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 新增、修改会员角色
     */
    public function addtype()
    {
        $model = new MemberTypeModel();
        $id = $this->request->has('member_type_id') ? $this->request->param('member_type_id', 0, 'intval') : 0;
        if ($id > 0) {
            //是修改操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();

                //折扣率
                $discoun_per_str = $post['discoun_per_list'];
                if($discoun_per_str!==''){
                    $discoun_per_arr = explode(",",$discoun_per_str);
                    foreach ($discoun_per_arr as $discoun_per_value) {
                        $per = substr($discoun_per_value,strpos($discoun_per_value,'_')+1);
                        $per_length = $this->_getFloatLength($per);
                        //验证折扣率
                        if(1<$per||$per<0||$per_length>2){
                            return $this->error('提交失败：折扣率输入有误');
                        }
                    }
                }

                //返佣利率
                $commission_per_str = $post['commission_per_list'];
                if($commission_per_str!==''){
                    $commission_per_arr = explode(",",$commission_per_str);
                    foreach ($commission_per_arr as $commission_per_value) {
                        $per = substr($commission_per_value,strpos($commission_per_value,'_')+1);
                        $per_length = $this->_getFloatLength($per);
                        //验证佣金率
                        if(1<$per||$per<0||$per_length>2){
                            return $this->error('提交失败：佣金率输入有误');
                        }
                    }
                }

                $post['updatetime']=time();

                if (false == $model->allowField(true)->save($post, ['member_type_id' => $id])) {
                    return $this->error('修改会员等级失败');
                } else {

                    return $this->success('修改会员等级成功', 'admin/membertype/index');
                }
            } else {
                //非提交操作
                $info = $model->where('member_type_id', $id)->find();
                $this->assign('info', $info);
                return $this->fetch();
            }
        } else {
            //是新增操作
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();

                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['type_name', 'require', '名称不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }

                //折扣率
                $discoun_per_str = $post['discoun_per_list'];
                if($discoun_per_str!==''){
                    $discoun_per_arr = explode(",",$discoun_per_str);
                    foreach ($discoun_per_arr as $discoun_per_value) {
                        $per = substr($discoun_per_value,strpos($discoun_per_value,'_')+1);
                        $per_length = $this->_getFloatLength($per);
                        //验证折扣率
                        if(1<$per||$per<0||$per_length>2){
                            return $this->error('提交失败：折扣率输入有误');
                        }
                    }
                }

                //验证名称是否存在
                $name = $model->where('type_name', $post['type_name'])->select();

                //返佣利率  字符串
                $commission_per_str = $post['commission_per_list'];
                if($commission_per_str!==''){
                    $commission_per_arr = explode(",",$commission_per_str);
                    foreach ($commission_per_arr as $commission_per_value) {
                        $per = substr($commission_per_value,strpos($commission_per_value,'_')+1);
                        $per_length = $this->_getFloatLength($per);
                        //验证佣金率
                        if(1<$per||$per<0||$per_length>2){
                            return $this->error('提交失败：佣金率输入有误');
                        }
                    }
                }

                if (!empty($name)) {
                    return $this->error('提交失败：角色名称已存在');
                }

                $post['creattime']=time();
                $post['updatetime']=time();

                if (false == $model->allowField(true)->save($post)) {
                    return $this->error('添加会员等级失败');
                } else {
                    return $this->success('添加会员等级成功', 'admin/membertype/index');
                }
            } else {
                //非提交操作
                return $this->fetch();
            }
        }
    }


        /**
     * 禁用/启用会员角色
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $model = new MemberTypeModel();
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            $status = $this->request->has('status') ? $this->request->param('status', 0, 'intval') : 0;
            if($status){
                $data['type_status'] =$status;
            }
            if($status==2){
                $msg='禁用';
            }else{
                $msg='启用';
            }
            $data['updatetime']=time();
            if (false == $model->allowField(true)->save($data, ['member_type_id' => $id])) {
                return $this->error($msg.'失败');
            } else {
                return $this->success($msg.'会员角色成功', 'admin/membertype/index');
            }
        }
    }


    /**
     * 设置折扣率
     */
    public function set_discoun()
    {
        $classify_info = Db::name('classify')->where("classify_status",1)->select();

        //加入包车和接送机
        $classify_info[] = array("classify_id"=>304,"classify_name"=>"接送机","classify_status"=>1,"creattime"=>0,"updatetime"=>0);
        $classify_info[] = array("classify_id"=>305,"classify_name"=>"包车","classify_status"=>1,"creattime"=>0,"updatetime"=>0);

        $this->assign('classify', $classify_info);
        return $this->fetch();
    }


    /**
     * 设置返佣利率
     */
    public function set_commission()
    {
        $classify_info = Db::name('classify')->where("classify_status",1)->select();

        //加入包车和接送机
        $classify_info[] = array("classify_id"=>304,"classify_name"=>"接送机","classify_status"=>1,"creattime"=>0,"updatetime"=>0);
        $classify_info[] = array("classify_id"=>305,"classify_name"=>"包车","classify_status"=>1,"creattime"=>0,"updatetime"=>0);

        $this->assign('classify', $classify_info);
        return $this->fetch();
    }


    //获取用户的 折扣率集
    public function get_discoun_per($member_id){
        //查询用户
        $member = db('member')->where('member_id',$member_id)->find();
        $all_per_arr = array();
        if($member['member_type_id']==1){
            return $all_per_arr;
        }

        $classify_info = Db::name('classify')->where("classify_status",1)->select();
        //加入包车和接送机
        $classify_info[] = array("classify_id"=>304,"classify_name"=>"接送机","classify_status"=>1,"creattime"=>0,"updatetime"=>0);
        $classify_info[] = array("classify_id"=>305,"classify_name"=>"包车","classify_status"=>1,"creattime"=>0,"updatetime"=>0);
        $classArr = array();
        foreach ($classify_info as $v) {
            $classArr[] = $v['classify_id'];
        }

        $where['member_type_id'] = $member['member_type_id'];
        $where['type_status'] = 1;
        $discoun_per = db('member_type')->where($where)->find();
        if(!empty($discoun_per)){
            //拆分 折扣率字符串
            $commission_per_list = $discoun_per['discoun_per_list'];
            if($commission_per_list && $commission_per_list!==''){
                $commission_per_arr = explode(",",$commission_per_list);
                foreach ($commission_per_arr as $per_value){
                    $per_arr = explode("_",$per_value);
                    $per_classify_id = $per_arr[0];
                    if(!in_array($per_classify_id,$classArr)){
                        continue;
                    }
                    $per = $per_arr[1];
                    $all_per_arr[$per_classify_id] = $per;
                }
            }
        }

        return $all_per_arr;
    }




}
